package com.zxl.yjb.activity.home.news;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.NewsAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LocationBean;
import com.zxl.yjb.bean.NewsBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LocationNewsActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.sm_refresh)
    SmartRefreshLayout sm_refresh;

    private DataManager dataManager;

    private RecyclerView.LayoutManager layoutManager;
    private NewsAdapter newsAdapter;
    private ArrayList<NewsBean> newsBeanList = new ArrayList<>();

    private int page = 1;
    private int count = 10;
    private double lon;
    private double lat;
    private LocationBean locationBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_news);
        ButterKnife.bind(this);

        locationBean = (LocationBean) SaveBeanUtil.getBean(this, Const.LOCATION_LNGLAT);
        if (locationBean != null) {
            lon = locationBean.getLng();
            lat = locationBean.getLat();
        }

        initView();
        getNews();
    }

    @Override
    protected void initView() {

        tv_page_title.setText("本地要闻");

        layoutManager = new LinearLayoutManager(this);
        rv_list.setLayoutManager(layoutManager);
        newsAdapter = new NewsAdapter(R.layout.item_news, newsBeanList);
        rv_list.setAdapter(newsAdapter);
        newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(LocationNewsActivity.this,NewsDetailActivity.class);
                intent.putExtra("id",newsBeanList.get(position).getId());
                startActivity(intent);
            }
        });

        sm_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getNews();

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getNews();
            }
        });

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();

                break;

            default:
                break;
        }

    }


    private void getNews() {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("count", count + "");
        map.put("lon", lon + "");
        map.put("lat", lat + "");
        ObserverOnNextListener<ArrayList<NewsBean>> listener = new ObserverOnNextListener<ArrayList<NewsBean>>() {
            @Override
            public void onNext(ArrayList<NewsBean> newsBeans) {
                if (page == 1)
                    newsBeanList.clear();
                newsBeanList.addAll(newsBeans);
                newsAdapter.notifyDataSetChanged();
                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        dataManager.newsList(new ProgressObserver<ArrayList<NewsBean>>(listener, this), map);
    }
}
